/*
 * 文 件 名 : SubmitQuestion.java
 * 版    权 : China Machi.Co.,Ltd.Copyright 2009-2011.All rights reserved
 * 描    述 : <描述>
 * 修 改 人 : lixg
 * 修改时间 : 2012-8-27
 * 跟踪单号 : <跟踪单号>
 * 修改单号 : <修改单号>
 * 修改内容 : <修改内容>
 */
package com.fire8.dolphin.service;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;
import android.widget.Toast;

import com.fire8.dolphin.db.QuestionDAO;
import com.fire8.dolphin.model.Question;
import com.fire8.dolphin.model.UserInfo;
import com.fire8.dolphin.ui.DolphinApplication;
import com.fire8.dolphin.utils.LogUtil;
import com.fire8.dolphin.utils.StringUtil;
import com.fire8.dolphin.web.RequestUtils;
import com.fire8.dolphin.web.UserInfoWeb;

/**
 * 提交问题
 * <功能详细描述>
 *
 * @author  lixg
 * @version [版本号,2012-8-27]
 * @see     [相关类/方法]
 * @since   [产品/模块]
 */
public class SubmitQuestion extends AsyncTask<Question, Void, String>
{
    final static String TAG = "dolphin";
    private StringBuffer url = new StringBuffer(DolphinApplication.URL);
    Context context;
    DolphinApplication app;
    String msg;
    public SubmitQuestion(Context c,DolphinApplication dApp)
    {
        this.context = c;
        app = dApp;
    }
    @Override
    protected String doInBackground(Question... params)
    {
        if(!RequestUtils.getInstall().isConnect(context))
        {
            return "网络不可用";
        }
        String userId = params[0].getUserId();
        //如果还没有生成用户信息，则
//        if(StringUtil.isEmpty(userId))
//        {
//            LogUtil.d("没有用户ID，先提交用户信息");
//            UserInfo userInfo = new UserInfo();
//            userInfo.setMobile(app.getTel());
//            userInfo.setMac(app.getMac());
//            userInfo.setAddress(app.getLocation());
//            UserInfoWeb web = new UserInfoWeb();
//            userId = web.submitUserInfo(userInfo);
//            app.setUserId(userId);
//            LogUtil.d("返回的用户ID："+userId);
//        }
        if(StringUtil.isEmpty(userId))
        {
            return "注册后才能提交问题";
        }
        LogUtil.d("提交问题");       
        QuestionDAO dao = new QuestionDAO(context);
        int len = params.length;
        for (int i = 0; i < len; i++)
        {
            Question question = params[i];
            question.setUserId(userId);
            String remoteId = submit(question);
            LogUtil.d("remoteId:"+remoteId);
            //将返回的远程ID更新到本地库
            if(!StringUtil.isEmpty(remoteId))
            {
                question.setRemoteId(remoteId);
                dao.update(question);
            }
        }
        return msg;
    }
    
    
    private String submit(Question question)
    {
        
        String remoteId=null;
        
        try
        {
            //上传语音
            if(!StringUtil.isEmpty(question.getVoice()))
            {
                LogUtil.d("上传语音");
                String path = RequestUtils.getInstall().multPartURL(question.getVoice(),null);
                if(!StringUtil.isEmpty(path))
                {
                    question.setVoice(path);
                }
            }
            
            //上传图片
            if(!StringUtil.isEmpty(question.getPicture()))
            {
                String path = RequestUtils.getInstall().multPartURL(question.getPicture(),null);
                if(!StringUtil.isEmpty(path))
                {
                    question.setPicture(path);
                }
            }
            
            
            List<NameValuePair> param = new ArrayList<NameValuePair>();
            param.add(new BasicNameValuePair("title",question.getTitle()));
            param.add(new BasicNameValuePair("context",question.getContext()));
            param.add(new BasicNameValuePair("userId",question.getUserId()));
            param.add(new BasicNameValuePair("voice",question.getVoice()));
            param.add(new BasicNameValuePair("picture",question.getPicture()));
            param.add(new BasicNameValuePair("voiceLong",question.getVoiceLong()+""));
            String json = RequestUtils.getInstall().getHttpJson(url.append("question/submitQuestion.php").toString(), param);
            LogUtil.d("question:"+json);
            JSONObject data = new JSONObject(json);
            String msgCode = data.getString("msgCode");
            String desc = data.getString("desc");
            if("1".equals(msgCode))
            {
                remoteId = desc;
                msg="提交成功";
            }else
            {
                msg = desc;
                Log.e(TAG, desc);
            }
        }
        catch (JSONException e)
        {
            Log.e(TAG, "" ,e);
        }
        
        return remoteId;
    }
    @Override
    protected void onPostExecute(String result)
    {
        super.onPostExecute(result);
        if(!StringUtil.isEmpty(result))
        {
            Log.d(TAG, result);
            Toast.makeText(context, result, Toast.LENGTH_LONG).show();
        }
    }
    
    
}
